package com.sugar.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 日志属性类
 * @author felix
 * @date 2025/7/4 15:37
 */

@Component
public class LogSystemPropertyInjector {

    // 冒号后为当前值不存在时的默认值
    @Value("${env.log.base:logs}")
    private String logBase;

    @Value("${env.log.retention.days:30}")
    private String logRetentionDays;

    @Value("${env.log.size.all:50MB}")
    private String logSizeAll;

    @Value("${env.log.size.warn:20MB}")
    private String logSizeWarn;

    @Value("${env.log.size.error:10MB}")
    private String logSizeError;

    @Value("${env.log.size.module:30MB}")
    private String logSizeModule;

    @Value("${spring.profiles.active:default}")
    private String activeProfile;


    @Value("${env.log.output.level:DEBUG}")
    private String outputLevel;

    @PostConstruct
    public void inject() {
        // 设置 JVM 级别的系统属性
        // 日志保存路径
        System.setProperty("log.base", logBase);
        // 日志过期时间
        System.setProperty("log.retention.days", logRetentionDays);
        // all日志归档阈值
        System.setProperty("log.size.all", logSizeAll);
        // warn日志归档阈值
        System.setProperty("log.size.warn", logSizeWarn);
        // error日志归档阈值
        System.setProperty("log.size.error", logSizeError);
        // 分模块日志归档阈值
        System.setProperty("log.size.module", logSizeModule);
        // 控制台日志输出开关，仅dev下输出
        System.setProperty("spring.profiles.active", activeProfile);
        // 日志输出级别
        System.setProperty("log.output.level", outputLevel);

        System.out.println("[LOG] 系统日志属性已注入: ");
        System.out.println("[LOG] 当前日志输出级别: " + outputLevel);
        System.out.println("[LOG] 日志保存路径: " + logBase);
        System.out.println("[LOG] 日志定时清理: " + logRetentionDays +"天");
        System.out.println("[LOG] all文件归档大小: " + logSizeAll);
        System.out.println("[LOG] 当前启动配置文件: " + activeProfile);
    }
}